<?php

/*
for subscribing a user to the updates of a group
depending upon the parameter $action, the user is either subscribed or unsubscribed from the group with id $gpid
*/

require_once("../../connection.php");

$fbid = $_POST['user'];
$gpid = $_POST['gpid'];
$action = $_POST['action'];

if($action == "subscribe")
{	
	$temp = mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
	if(mysql_numrows($temp)!=0)  // that means there is no record whatsoever
		$res = mysql_query("UPDATE site_subscribe SET `del`='0' WHERE `fbid`='$fbid' AND `gpid`='$gpid'");
	else // means there is a record, hence set the del parameter to 0 whatever its initial value be
		$res = mysql_query("INSERT INTO site_subscribe (`fbid`,`gpid`,`del`) VALUES('$fbid','$gpid','0')");
	
	if(!$res) echo  mysql_error();
}
else if($action == "unsubscribe")
{
	$temp = mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
	if(mysql_numrows($temp)!=0) // if this values is 0, then there is no need to do any further action
		$res = mysql_query("UPDATE site_subscribe SET `del`='1' WHERE `fbid`='$fbid' AND `gpid`='$gpid'");
	
	if(!$res) echo  mysql_error();
}

?>